Skip to content

Remove whitespace #25492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
Closed

Remove whitespace #25492

wants to merge 16 commits into from

Conversation

rbuckton
Copy link
Contributor

@rbuckton rbuckton commented Jul 6, 2018

This PR adds a --removeWhitespace compiler switch that affects JavaScript output only. When enabled, any insignificant whitespace (along with any insignificant trailing semicolon characters) is elided from the output.

Whitespace is considered significant in the following cases:

  • Between a keyword or identifier and another keyword or identifier.
  • Between a keyword or identifier and a number.
  • Between + and +
  • Between + and ++
  • Between - and -
  • Between - and --
  • Line terminators following a single-line comment

Semicolons are considered insignificant in the following cases:

  • The last ; before then end of a block (}), except when part of an embedded statement (i.e. {while(true);})
  • A run of multiple trailing semicolons (;;), except when in the head of a for loop (i.e. for(;;))

writer.writeProperty(s);
}

function writeSignificantLine() {
// writer.flush();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should either comment on why there's commented out code here or remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be removed.

&& typeof x.file === "string"
&& typeof x.mappings === "string"
&& isArray(x.sources) && every(x.sources, isString)
&& (x.sourceRoot === undefined || typeof x.sourceRoot === "string")
Copy link
Member

@weswigham weswigham Jul 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we handle if sourceRoot === null? Same for sourcesContent and names?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think null is legal per the spec, but we can be more permissive.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather make a best effort to parse a variety of bad sourcemaps than stubbornly refuse some almost correct ones.

return x !== null
&& typeof x === "object"
&& x.version === 3
&& typeof x.file === "string"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While version and file are technically required in the spec.... we don't use them, and we don't need them to be present to handle a map correctly. Do we really need to check for them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its a small enough test on a non-hot code-path, and I'd prefer to align with the spec here.

@@ -1,4 +1,3 @@
/* @internal */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removal intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Otherwise we get test failures for our Public API tests after building tsserverlibrary.d.ts.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these types supposed to be public (on master the library build is borked and including all private types)? This comment implies not...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They actually were public prior to the change to use project references:

https://github.com/Microsoft/TypeScript/blob/release-2.7/lib/tsserverlibrary.d.ts#L4845

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh. OK. I guess we should ask Ryan if he intended to make them private or not.

Copy link
Contributor Author

@rbuckton rbuckton Jul 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the issue is that they weren't "public" in typescriptServices.d.ts/typescript.d.ts, but they are "public" in tsserverlibrary.d.ts. Unfortunately, we can't make a file conditionally internal.

@RyanCavanaugh - What's the best course of action here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the jsTyping file now contributes to the services build? Huh. I imagine it should be public then, considering they were already public in the server library; unless we've got a good reason to keep it private (at which point it should be private in both).

@@ -1,21 +1,29 @@
/*! *****************************************************************************
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comments in this file are now gone... except for the copyright header, which is now here. Seems wrong?

var a=0,b,{c}=obj,[d]=obj;let e=0,f,{g}=obj,[h]=obj;
//# sourceMappingURL=variables.js.map//// [for.js]
for(;;){}
//# sourceMappingURL=for.js.map//// [embeddedStatement.js]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. There's no tailing newline in the file, which is the goal, but this baseline is ugly because there's now a //// header on the same line as the last line as the prior file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We weren't previously emitting a trailing newline after the source map comment. You can see this in existing baselines like jsxFactoryIdentifier.js, jsxFactoryQualifiedName.js, etc. We could probably fix this, but I wanted to avoid unrelated baseline changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:( we should open an issue to track it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll just go ahead and fix it.

@rbuckton
Copy link
Contributor Author

@sandersn, @mhegazy - If you have some time can you take a look?

@rbuckton rbuckton mentioned this pull request Jul 10, 2018
@rbuckton
Copy link
Contributor Author

@sandersn Should be smaller now with the build script changes pulled out.

Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine as far as I could understand it with one reading. Maybe tomorrow I can sit down with you to learn some higher-level concepts to do with emitting and source maps.

@@ -61,11 +61,10 @@ namespace ts {
let str = "";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not worth fixing as part of this PR, but utilities is a weird place for creating EmitTextWriters. Seems pretty specialised.

let nameToNameIndexMap: Map<number> | undefined;

// Last recorded and encoded mappings
let generatedLine = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit confusing seeing a lot of references to generatedLine, sourceIndex, etc even though most are to locals, not to these closed-over declarations. Maybe another prefix to contrast with 'pending-' would help?

}

enter();
Debug.assert(pendingGeneratedCharacter >= 0, "lastRecordedGeneratedCharacter was negative");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand how the assert text relates to the assertion.

@rbuckton
Copy link
Contributor Author

@weswigham if you have time can you take another look?

Copy link
Contributor

@mhegazy mhegazy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weswigham can you take another look.

@rbuckton
Copy link
Contributor Author

@mhegazy should I wait on @weswigham or merge?

@mhegazy
Copy link
Contributor

mhegazy commented Jul 12, 2018

go for it.

@rbuckton
Copy link
Contributor Author

@DanielRosenwasser indicated we might want to wait on this a bit longer. I'll hold off on merging until we can discuss.

@weswigham
Copy link
Member

Just one comment about the remove whitespace output: By it's nature, it is very hard to read (I'm pretty sure I just glossed over the new output baselines). It would be best if we could verify that they still behave as expected post-de-whitespacing. It's not necessary, strictly speaking, but just like with helper changes, these are exactly the kind of emit-related changes that actually validating the runtime behavior of the generated code seems useful to me. Just another remark for the technical backlog~ ❤️

@sandersn sandersn added the Experiment A fork with an experimental idea which might not make it into master label Feb 1, 2020
@sandersn
Copy link
Member

This experiment is pretty old, so I'm going to close it to reduce the number of open PRs.

@sandersn sandersn closed this May 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team Experiment A fork with an experimental idea which might not make it into master
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants